﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace CWCS.Common
{
    public class Helper
    {
        public static byte[] GetHexBytes(string info)
        {
            try
            {
                //将一个整数转化为长度为2的byte型数组
                byte[] Buffer = new byte[2];
                int i = int.Parse(info);
                Buffer[0] = (byte)(i / 256);
                Buffer[1] = (byte)(i % 256);
                return Buffer;
            }
            catch (Exception )
            { return null; }
        }

    }

    public class TimerManager
    {
        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceFrequency(out long lpFrequency);
        private long startTime, stopTime;
        private long freq;
        public TimerManager()
        {
            startTime = 0; stopTime = 0;
            if (QueryPerformanceFrequency(out freq) == false)
            {
                //throw new Exception("Timer not supported.");
            }
        }
        public void Start()
        {
            Thread.Sleep(0);
            QueryPerformanceCounter(out startTime);
        }
        public void Stop()
        {
            QueryPerformanceCounter(out stopTime);
        }
        public double Duration
        {
            get { return (double)(stopTime - startTime) / (double)freq; }
        }
    }
}
